package com.itheima.mall.service.smsService;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.dto.SmsCouponDto;
import com.itheima.pojo.sms.SmsCoupon;
import com.itheima.pojo.sms.SmsCouponProductCategoryRelation;
import com.itheima.pojo.sms.SmsCouponProductRelation;
import com.itheima.mall.commons.PageBean;
import com.itheima.mall.commons.R;
import com.itheima.mall.exception.ConsumerException;
import com.itheima.mall.service.smsService.SmsCouponProductCategoryRelationApi;
import com.itheima.mall.service.smsService.SmsCouponProductRelationApi;
import com.itheima.mall.service.smsService.SmsCouponServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsCouponService {
    @DubboReference
    private SmsCouponServiceApi smsServiceApi;
    @DubboReference
    private SmsCouponProductRelationApi smsCouponProductRelationApi;
    @DubboReference
    private SmsCouponProductCategoryRelationApi smsCouponProductCategoryRelationApi;
    //添加优惠劵
    public void createCoupon(SmsCouponDto smsCouponDto) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtil.copyProperties(smsCouponDto, smsCoupon);
        smsServiceApi.createCoupon(smsCoupon);
        SmsCouponProductRelation smsCouponProductRelation = new SmsCouponProductRelation();
        if (smsCouponProductRelation != null) {
            List<SmsCouponProductRelation> productRelationList = smsCouponDto.getProductRelationList();
            for (SmsCouponProductRelation couponProductRelation : productRelationList) {
                smsCouponProductRelationApi.createCoupon(couponProductRelation);
            }
        }
        SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = new SmsCouponProductCategoryRelation();
        if (smsCouponProductCategoryRelation != null) {
            List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponDto.getProductCategoryRelationList();
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelationApi.createCoupon(couponProductCategoryRelation);
            }
        }
    }

    //删除优惠卷
    public void deleteCreate(Integer id) {
        if (id == null) {
            throw new ConsumerException("id不存在");
        }
        smsServiceApi.deleteCreate(id);
    }
    //根据优惠卷名称和类型分页获取优惠卷列表
    public R getList(String name, Integer pageNum, Integer pageSize, Integer type) {
        PageBean<SmsCoupon> createLisrt = smsServiceApi.getList(name, pageNum, pageSize, type);
        return R.ok(createLisrt);
    }
    //修改优惠卷
    public void updateCreate(Integer id, SmsCouponDto smsCouponDto) {
        SmsCoupon smsCoupon = new SmsCoupon();
        smsCoupon.setId(Convert.toLong(id));
        BeanUtil.copyProperties(smsCouponDto, smsCoupon);
        smsServiceApi.updateByIds(smsCoupon);
        SmsCouponProductRelation smsCouponProductRelation = new SmsCouponProductRelation();
        if (smsCouponProductRelation != null) {
            List<SmsCouponProductRelation> productRelationList = smsCouponDto.getProductRelationList();
            for (SmsCouponProductRelation couponProductRelation : productRelationList) {
                smsCouponProductRelationApi.updateWithId(couponProductRelation);
            }
        }
        SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = new SmsCouponProductCategoryRelation();
        if (smsCouponProductCategoryRelation != null) {
            List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponDto.getProductCategoryRelationList();
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelationApi.updateByIds(couponProductCategoryRelation);
            }
        }
    }

    //获取单个优惠卷详细信息
    public R CouponById(Integer id) {
        SmsCoupon smsCoupon = smsServiceApi.getById(id);
        List<SmsCouponProductRelation> list = smsCouponProductRelationApi.getByCoupon(smsCoupon.getId());
        List<SmsCouponProductCategoryRelation> lists = smsCouponProductCategoryRelationApi.getByCoupon(smsCoupon.getId());
        SmsCouponDto smsCouponDto = new SmsCouponDto();
        smsCouponDto.setProductRelationList(list);
        smsCouponDto.setProductCategoryRelationList(lists);
        BeanUtil.copyProperties(smsCoupon, smsCouponDto);

        return R.ok(smsCouponDto);

    }
}
